package com.stox.dto;

import java.io.Serializable;
import java.util.Date;

import lombok.Data;

import org.codehaus.jackson.annotate.JsonIgnoreProperties;

import com.stox.util.Constant;

@Data
@JsonIgnoreProperties(ignoreUnknown=true)
public abstract class AbstractBar implements Serializable{
	private static final long serialVersionUID = 1L;

	private Long scripId;

	private Date date;

	private double open;

	private double high;

	private double low;

	private double close;

	private double volume;

	public AbstractBar() {
		
	}

	public AbstractBar(Long scripId, String text) {
		String[] tokens = text.split(",");
		setScripId(scripId);
		try {
			setDate(Constant.dateFormat.parse(tokens[0]));
			setOpen(Double.parseDouble(tokens[1]));
			setHigh(Double.parseDouble(tokens[2]));
			setLow(Double.parseDouble(tokens[3]));
			setClose(Double.parseDouble(tokens[4]));
			setVolume(Double.parseDouble(tokens[5]));
		} catch (Exception e) {
			System.out.println(scripId+","+text);
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	@Override
	public String toString() {
		return Constant.dateFormat.format(date) + "," + open + "," + high + ","
				+ low + "," + close + "," + volume;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		AbstractBar other = (AbstractBar) obj;
		if (date == null) {
			if (other.date != null)
				return false;
		} else if (!date.equals(other.date))
			return false;
		if (scripId == null) {
			if (other.scripId != null)
				return false;
		} else if (!scripId.equals(other.scripId))
			return false;
		return true;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((date == null) ? 0 : date.hashCode());
		result = prime * result
				+ ((scripId == null) ? 0 : scripId.hashCode());
		return result;
	}
	
}
